/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.registration.reports;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.registration.core.Attendee;
import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.apache.commons.lang.StringUtils.isBlank;

/**
 * @date Jun 27, 2012
 *
 * @author Ggutierrez
 */
public class LabelToPdf {

    Map messages;
    Image logo;
    Format longFormat = new SimpleDateFormat("dd MMM yyyy");
    Format formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    Format simpleFormat = new SimpleDateFormat("dd/MM/yyyy");
    PdfWriter writer;
    Font fBoldItalic = new Font(Font.HELVETICA, 12, Font.BOLDITALIC);
    Font fNormal = new Font(Font.HELVETICA, 12, Font.NORMAL);
    Font fUnderline = new Font(Font.HELVETICA, 12, Font.UNDERLINE);
    Font fItalic = new Font(Font.HELVETICA, 12, Font.ITALIC);
    Font fBold = new Font(Font.HELVETICA, 12, Font.BOLD);

    public LabelToPdf() {
        Properties props = new Properties();
        try {
            logo = Image.getInstance(getClass().getResource("/logo_concapanxxxii_small.png"));
            messages = props;
        } catch (BadElementException ex) {
            Logger.getLogger(PreInscriptionToPdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(PreInscriptionToPdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PreInscriptionToPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void write(Attendee attendee, OutputStream out) {
        float margin = 20;
        float marginTop = 50f;
        float marginBotom = 0;
        Rectangle pageSize = new Rectangle(250f, 300f);
        Document document = new Document(pageSize, margin, margin, marginTop, marginBotom);
        try {
            writer = PdfWriter.getInstance(document, out);

            document.open();

            document.add(blankLine());
            addContent(document, attendee);

            document.close();

        } catch (DocumentException ex) {
            Logger.getLogger(PreInscriptionToPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addContent(Document document, Attendee attendee) throws DocumentException {
        float[] widths = {0.65f};
        Color color = Color.BLACK;

        PdfPTable content = new PdfPTable(widths);
        content.setWidthPercentage(100);
        content.getDefaultCell().setBorder(0);
        content.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

        String nickName = !isBlank(attendee.getNickName()) ? attendee.getNickName() : attendee.getFirstName();
        String category = attendee.getCategory() != null ? attendee.getCategory().getDescription() : "Sin categoria";
        String country = attendee.getCountry() != null ? attendee.getCountry().getName() : "";
        String branch = attendee.getBranch() != null ? attendee.getBranch() : "";

        if (attendee.getCategory() != null && !isBlank(attendee.getCategory().getColor())) {
            String colorCat = attendee.getCategory().getColor().toUpperCase();
            color = Color.getColor(colorCat);
        }

        content.addCell(new Paragraph(nickName, new Font(Font.HELVETICA, 28, Font.BOLD)));
        content.addCell(new Paragraph("   ", new Font(Font.HELVETICA, 28, Font.BOLD)));
        content.addCell(new Paragraph(branch, new Font(Font.HELVETICA, 16, Font.BOLD, color)));
        content.addCell(new Paragraph("   ", new Font(Font.HELVETICA, 28, Font.BOLD)));
        content.addCell(new Paragraph(country, new Font(Font.HELVETICA, 16, Font.BOLD)));        

        document.add(content);
    }

    public Paragraph blankLine() {
        //White Line
        String text = " ";
        Paragraph s = new Paragraph(text);
        s.setAlignment(Element.ALIGN_CENTER);
        return s;
    }
}
